package com.obtk.property.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.obtk.property.dao.HisBaseDictMapper;
import com.obtk.property.pojo.ChargeItem;
import com.obtk.property.pojo.HisBaseDict;
import com.obtk.property.service.HisBaseDictService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;

public class HisBaseDictServiceImpl implements HisBaseDictService {
    @Autowired
    private HisBaseDictMapper hisBaseDictMapper;
    @Override
    public List<HisBaseDict> findAll() {
        return hisBaseDictMapper.selectAll();
    }

    @Override
    public Page<HisBaseDict> search(Map searchMap) {
        //通用Mapper多条件搜索，标准写法
        Example example = new Example(ChargeItem.class);//指定查询的表
        //1.初始化分页条件
        int pageNum = 1;
        int pageSize = 2;
        if(searchMap != null){
            Example.Criteria criteria = example.createCriteria();//创建查询条件
            //时间区间
            if(StringUtil.isNotEmpty((String) searchMap.get("startTime"))){
                criteria.andGreaterThanOrEqualTo("createTime",searchMap.get("startTime"));
            }
            if(StringUtil.isNotEmpty((String) searchMap.get("endTime"))){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("endTime"));
            }
            //名称模糊搜索
            if(StringUtil.isNotEmpty((String) searchMap.get("name"))){
                criteria.andLike("name", "%"+(String) searchMap.get("name")+"%");
            }
            //分页

            if((Integer) searchMap.get("pageNum") !=null){
                pageNum = (Integer) searchMap.get("pageNum");
            }
            if((Integer) searchMap.get("pageSize") !=null){
                pageSize = (Integer) searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);//使用PageHelper插件完成分页
        Page<HisBaseDict> hisBaseDicts = (Page<HisBaseDict>) hisBaseDictMapper.selectByExample(example);
        return hisBaseDicts;
    }



    @Override
    public Boolean add(HisBaseDict hisBaseDict) {
        int row = hisBaseDictMapper.insert(hisBaseDict);
        if(row>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public HisBaseDict findById(Integer chargeItemId) {
        return hisBaseDictMapper.selectByPrimaryKey(chargeItemId);
    }



    @Override
    public Boolean update(HisBaseDict hisBaseDict) {
        int row=hisBaseDictMapper.updateByPrimaryKeySelective(hisBaseDict);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean del(List<Integer> ids) {
        for(Integer id:ids){
            hisBaseDictMapper.deleteByPrimaryKey(id);

        }
        return true;
    }
}
